package com.four.reptile.config;

import com.four.reptile.entity.Reptile;
import com.four.reptile.service.ReptileService;
import com.four.reptile.service.impl.ReptileServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import us.codecraft.webmagic.ResultItems;
import us.codecraft.webmagic.Task;
import us.codecraft.webmagic.pipeline.Pipeline;

import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.util.Map;

@Component
public class JsonFilePipeline implements Pipeline {
    @Override
    public void process(ResultItems resultItems, Task task) {
        Object name = resultItems.get("name");
        Object img = resultItems.get("img");
        Object price = resultItems.get("price");
        System.out.println("===>>"+resultItems);
        try {
            Class.forName("com.mysql.jdbc.Driver");
            Connection conn = DriverManager.getConnection("jdbc:mysql://139.196.190.19:3306/four_reptile?characterEncoding=utf-8&serverTimezone=GMT%2B8","root","root");
            PreparedStatement ps = conn.prepareStatement("insert into tb_reptile(title,img,price) values('" + name + "','https:" + img + "','" + price + "')");
            int l = ps.executeUpdate();
            if (l > 0) {
                System.out.println("添加成功");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
